/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit.regexp;

import org.evswork.getwit.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.widget.EditText;

public class RegExpEditDialogMaker {
	AlertDialog.Builder builder;
	LayoutInflater inflater;
	
	RegExpView v;
	EditText edit;
	RegExpListAdapter adapter;
	
	public RegExpEditDialogMaker(RegExpView v,RegExpListAdapter adapter) {
		this.v = v;
		this.adapter = adapter;
		builder = new AlertDialog.Builder(v.getContext());
		inflater = LayoutInflater.from(v.getContext());
		makeDialog();
	}

	private void makeDialog() {
		edit = (EditText)inflater.inflate(R.layout.regexp_edit, null);
		edit.setText(v.data.data);
		builder
			.setTitle(String.format("编辑规则 #%d", v.data.pos))
			.setView(edit)
			.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog,
						int which) {
					v.data.update(edit.getText().toString());
					adapter.notifyDataSetChanged();
				}
			})
			.setNegativeButton("取消", null)
			.show();
	}
}
